﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Server {
	public static class FileLogger {
		private static DateTime appStart = DateTime.Now;

		public static void SaveTest(TestResult result) {
			String path = "tests/" + appStart.ToString("yyyy\\/MM\\/dd\\/HHmmssff\\/") + result.Name + "/";
			if (!Directory.Exists(path)) {
				Directory.CreateDirectory(path);
			}
			String filename = String.Format("{1}-{2}_{0}", result.Node.ToString(), result.StartTime.ToString("HHmmssff"), result.EndTime.ToString("HHmmssff"));

			File.WriteAllText(
				path + filename + ".csv",
				result.ResultsHeader + "\n" + String.Join("\n", result.Results.ToArray()),
				Encoding.UTF8
			);
			File.WriteAllText(
				path + filename + ".description",
				result.Description,
				Encoding.UTF8
			);

			Logger.Test(String.Format("Results of given test has been saved in {0}, next to Server executable file. Description of this test has the same filename, but \".description\" extension.", path + filename + ".csv"));
		}

		public static void SaveLog(Guid nodeGuid, String[] logs) {
			if (null == logs || logs.Length < 2) {
				return;
			}
			String path = "logs/" + appStart.ToString("yyyy\\/MM\\/dd\\/HHmmssff\\/");
			if (!Directory.Exists(path)) {
				Directory.CreateDirectory(path);
			}
			String filename = String.Format("{0}.log", nodeGuid.ToString());
			File.WriteAllText(path + filename, String.Join("\n", logs), Encoding.UTF8);

		}
	}
}
